package com.example.ktapplication.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.ktapplication.R;
import com.example.ktapplication.bean.FindArticleDataX;

import java.util.ArrayList;
import java.util.List;

public class RvResultAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<FindArticleDataX> mArticleList = new ArrayList();
    private LayoutInflater mLayoutInflater;
    private Context mContext;
    private OnItemClickListener itemClickListener;

    public RvResultAdapter(Context context) {
        mContext = context;
        mLayoutInflater = LayoutInflater.from(context);
    }

    @SuppressLint("NotifyDataSetChanged")
    public void setData(List<FindArticleDataX> aricleList) {
        mArticleList.clear();
        mArticleList.addAll(aricleList);
        notifyDataSetChanged();
    }

    public void updateItem(FindArticleDataX dataX) {
        int index = mArticleList.indexOf(dataX);
        if (index != -1) {
            mArticleList.set(index, dataX);
            notifyItemChanged(index);
        }
    }

    public void setItemClickListener(OnItemClickListener clickListener) {
        itemClickListener = clickListener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ContentViewHolder(mLayoutInflater
                .inflate(R.layout.rv_result_item_layout, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        FindArticleDataX bean = mArticleList.get(position);
        ContentViewHolder viewHolder = (ContentViewHolder) holder;
        viewHolder.tvAuthor.setText("作者:" + (bean.getAuthor().isEmpty() ? "未知" : bean.getAuthor()));
        viewHolder.tvTitle.setText(bean.getTitle());
        viewHolder.tvTime.setText("时间:" + (String.valueOf(bean.getNiceShareDate())));
        viewHolder.tvLable.setText("来源:" + bean.getSuperChapterName());
        if (bean.getCollect()) {
            Glide.with(mContext)
                    .load(R.drawable.icon_collect)
                    .into(viewHolder.ivColllect);
        } else {
            Glide.with(mContext)
                    .load(R.drawable.icon_un_collect)
                    .into(viewHolder.ivColllect);
        }
        viewHolder.ivColllect.setOnClickListener(view -> {
            if (itemClickListener != null) {
                itemClickListener.onCollectClick(bean);
            }
        });
        viewHolder.llBody.setOnClickListener(view -> {
            ///跳转WEB
            if (itemClickListener != null) {
                itemClickListener.onBodyClick(bean);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mArticleList.size();
    }

    //内容 ViewHolder
    public class ContentViewHolder extends RecyclerView.ViewHolder {

        private TextView tvTitle;
        private TextView tvAuthor;
        private TextView tvTime;
        private TextView tvLable;
        private ImageView ivColllect;
        private LinearLayout llBody;

        public ContentViewHolder(View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.txtTitle);
            tvAuthor = itemView.findViewById(R.id.txtAuthor);
            tvTime = itemView.findViewById(R.id.txtTime);
            tvLable = itemView.findViewById(R.id.tvtLable);
            ivColllect = itemView.findViewById(R.id.imgCollect);
            llBody = itemView.findViewById(R.id.llBody);
        }
    }

    public interface OnItemClickListener {
        void onCollectClick(FindArticleDataX item);

        void onBodyClick(FindArticleDataX item);
    }

}
